package company;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import offer.Offer;

import city.City;
import city.CityHome;

import recruiter.Recruiter;
import sector.Sector;
import sector.SectorHome;
import student.Student;

import compsup.CompanySup;
import country.Country;
import country.CountryHome;

import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.OfferListDescriptor;

/**
 * CompanyBean
 * 
 * 
 * The CompanyBean class implements all the methods that a remote client expect to find
 * when it calls the interface (Company).
 * 
 * Note :  -We use @SuppressWarnings("unchecked") to avoid the useless warnings
 */
@SuppressWarnings("unchecked")
public abstract class CompanyBean implements EntityBean{
	
	City city = null;
	CityHome cityHome = null;
	Country country = null;
	CountryHome countryHome = null;
	Sector sector = null;
	SectorHome sectorHome = null;
	InitialContext initialContext = null;
	// <ATTRIBUTES>
	
	// simple attributes
	public abstract int getId();
	public abstract void setId(int id);
	
	public abstract String getName();
	public abstract void setName(String name);
	
	// Bean attributes

	// relation 1-many
	public abstract Collection getRecruiters();
	public abstract void setRecruiters(Collection recruiters);
	
	public abstract Collection getCompanySups();
	public abstract void setCompanySups(Collection companySups);
	
	public abstract Collection<Offer> getOffers();
	public abstract void setOffers(Collection<Offer> offers);
	
	public abstract Collection<Student> getInterestedStudents();
	public abstract void setInterestedStudents(Collection<Student> interestedStudents);
	
	public abstract String getAddress();
	public abstract void setAddress(String address);
	
	public abstract String getPhone();
	public abstract void setPhone(String phone);
	
	public abstract String getEmail();
	public abstract void setEmail(String email);
	
	public abstract Sector getSector();
	public abstract void setSector(Sector sector);
	
	public abstract City getCity();
	public abstract void setCity(City city);
	
	public abstract Country getCountry();
	public abstract void setCountry(Country country);
	// </ATTRIBUTES>
	
	// <BEANS METHODS>
	
	public String ejbCreate(String name, String address, String phone, String email, String cityName, String countryName, String sectorName) throws CreateException {
		setName(name);
		setAddress(address);
		setPhone(phone);
		setEmail(email);
		try 
		{
			initialContext = new InitialContext();
			countryHome = (CountryHome)initialContext.lookup("MyCountry_L");
			cityHome = (CityHome)initialContext.lookup("MyCity_L");
			sectorHome = (SectorHome)initialContext.lookup("MySector_L");
			country = countryHome.findByName(countryName);
			city = cityHome.findByName(cityName);
			sector = sectorHome.findByName(sectorName);
		} 
		catch (FinderException e) 
		{
			if(country == null)
			{
				country = countryHome.create(countryName);
			}
			if(city == null)
			{
				city = cityHome.create(cityName, country);
			}
			if(sector == null)
			{
				sector = sectorHome.create(sectorName);
			}
		} 
		catch (NamingException e) {e.printStackTrace();}
		
		return null;
	}

	
	public void ejbPostCreate(String name, String address, String phone, String email, String cityName, String countryName, String sectorName){
		setRecruiters(new Vector());
		setCompanySups(new Vector());
		setInterestedStudents(new Vector());
		if(country != null)
		{
			setCountry(country);
		}
		if(city != null)
		{
			setCity(city);
		}
		if(sector != null)
		{
			setSector(sector);
		}
	}
	
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbRemove() throws EJBException, RemoteException,RemoveException {}
	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}
	public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}
	public void unsetEntityContext() throws EJBException, RemoteException {}
	
	// </ BEANS METHODS>
	
	public CompanyDescriptor getDescriptor(){
		int [] studentIds = new int[this.getInterestedStudents().size()];
		int i = 0;
		for (Object o:getInterestedStudents()){
			studentIds[i] = ((Student)o).getId();
			i++;
		}
		int [] supervisorIds = new int[this.getCompanySups().size()];
		i = 0;
		for (Object o:getCompanySups()){
			supervisorIds[i] = ((CompanySup)o).getId();
			i++;
		}
		int [] recruiterIds = new int[this.getRecruiters().size()];
		i = 0;
		for (Object o:getRecruiters()){
			recruiterIds[i] = ((Recruiter)o).getId();
			i++;
		}
		return new CompanyDescriptor(getId(),getName(),getAddress(), getPhone(),getEmail(), getCity().getName(),getCountry().getName(), getSector().getName(),getOffers().size(),studentIds,recruiterIds,supervisorIds);
		//return new CompanyDescriptor(getId(),getName(),getAddress(), getPhone(),getEmail(), "","", "",getOffers().size(),studentIds,recruiterIds,supervisorIds);
		
	}
	
	
	
	public CompanyDescriptor getSimpleDescriptor(){
		//int [] studentIds = new int[this.getInterestedStudents().size()];
		int i = 0;
		
		int [] supervisorIds = new int[this.getCompanySups().size()];
		i = 0;
		for (Object o:getCompanySups()){
			supervisorIds[i] = ((CompanySup)o).getId();
			i++;
		}
		int [] recruiterIds = new int[this.getRecruiters().size()];
		i = 0;
		for (Object o:getRecruiters()){
			recruiterIds[i] = ((Recruiter)o).getId();
			i++;
		}
		//return new CompanyDescriptor(getId(),getName(),getAddress(), getPhone(),getEmail(), getCity().getName(),getCountry().getName(), getSector().getName(),getOffers().size(),studentIds,recruiterIds,supervisorIds);
		return new CompanyDescriptor(getId(),getName(),getAddress(), getPhone(),getEmail(), getCity().getName(),getCountry().getName(), getSector().getName(),getOffers().size(),new int[0],recruiterIds,supervisorIds);
		
	}

}
